package com.leave.mos.controller.simple;

import com.leave.mos.constant.TablenameConstant;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ComparisonQueryOperatorsController
 * @Description TODO
 * @Author lh
 * @Date 2021/7/12 16:50
 * @Version 1.0
 *
 * ArrayQueryOperators 数组查询运算符¶
 *
 **/
@RestController
@RequestMapping("array")
public class ArrayQueryOperatorsController {

    @Resource
    private MongoTemplate mongoTemplate;
    /**
     *
     * @return
     */
    @RequestMapping("all")
    public List<Map> all(){

        //在$all操作者选择的文件，其中一个字段的值是包含所有指定元素的数组
        Criteria criteria1 = new Criteria(TablenameConstant.goods_order.boxId).all(9001,9008);
        Query query = new Query();
        query.addCriteria(criteria1);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    /**
     *
     * @return
     */
    @RequestMapping("elemMatch")
    public List<Map> elemMatch(){

        //运算符匹配包含数组字段的文档，其中至少有一个元素与所有指定的查询条件匹配。
        Criteria c = new Criteria().gt(9004).lt(900001);
        Criteria criteria1 = new Criteria(TablenameConstant.goods_order.boxId).elemMatch(c);
        Query query = new Query();
        query.addCriteria(criteria1);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    /**
     *
     * @return
     */
    @RequestMapping("size")
    public List<Map> size(){

        //该$size运算符匹配具有参数指定的元素数量的任何数组
        //配置数组中只有两个元素的 文档
        //在$all操作者选择的文件，其中一个字段的值是包含所有指定元素的数组
        Criteria criteria1 = new Criteria(TablenameConstant.goods_order.boxId).size(2);
        Query query = new Query();
        query.addCriteria(criteria1);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }



}
